<?php

namespace App\Transformers\Base;

use App\Models\Base\ChinaArea;
use App\Models\Base\MemberAlbum;
use App\Models\Base\Message;
use League\Fractal\ParamBag;
use League\Fractal\TransformerAbstract;

class MessageTransformer extends TransformerAbstract
{
//    protected $availableIncludes = ['user',  'recentComments'];

    public function transform(Message $message)
    {
//        return $message->attributesToArray();

        $sender = $message->sender_id;
//        $senderName = '';
        $senderImg = ''; //默认头像
        $senderAge = '';
        $senderAddress = '';

        if($sender === 0) //系统消息
        {
            $senderName = '系统小秘书';
        }
        else
        {
            $album = MemberAlbum::where('member_id',$message->sender_id)->first();
            $albumImg  = ''; //默认头像
            if(!empty($album))
            {
                $albumImg = $album->img_url;
            }
            $senderInfo = $message->sender;
            $senderName = '';
            $senderAge = '';
            $senderAddress = '';
            if($senderInfo)
            {
//                $senderInfo = $message->sender->info;
                $senderName = $message->sender->info->true_name;
                $senderAge = $message->sender->info->age;
                $senderAddress = $this->getAddressCn($message->sender->info->household_register_address);
            }
            $senderImg = $albumImg;

        }

        return [
            'id' => $message->id,
            'member_id' => $message->member_id,
            'sender_id' => $message->sender_id,
            'sender_name' => $senderName,
            'sender_img' => $senderImg,
            'sender_age' => $senderAge,
            'sender_address' => $senderAddress,
            'title' => $message->title,
            'content' => $message->content,
            'is_read' => $message->is_read,
            'createtime' => $message->createtime,
//            'createtime' => $message->createtime->toDateTimeString(),
//            'updated_at' => $message->updated_at->toDateTimeString(),
        ];
}



    function getAddressCn($address)
    {

        if($address)
        {
            $addressArr = json_decode($address);
        }
        if(!empty($addressArr))
        {
            //        Log::info($address);
//        $memberCommon->address;
            $area = ChinaArea::whereIn("id", $addressArr)
              ->select("id", "name")->orderBy('id')->get();
            if($area) return $this->getAllAddress($area);
            return '';
//            Log::info($memberCommon->address);
        }
        return '';
    }

    function getAllAddress($addressArr)
    {
        $return = "";
        foreach ($addressArr as $address) {
            if ($address->name != "市辖区") {
                $return .= $address->name;
            }
        }
        return $return;

    }


//  如果有包含子对象，可重写此方法
//    public function includeUser(Post $message)
//    {
//        if (! $post->user) {
//            return $this->null();
//        }
//
//        return $this->item($post->user, new UserTransformer());
//    }


    /**
     * 列表加载列表不是一件很好的事情，因为dingo的预加载机制
     * 自动预加载include的参数, 所以会读取所有帖子的所有评论
     * 所以可以增加一个recentComments, 增加一个limit条件
     * 但是依然不够完美.
     */
//    public function includeRecentComments(Post $post)
//    {
//        // 倒叙
//        $comments = $post->recentComments->sortByDesc('id');
//
//        return $this->collection($comments, new CommentTransformer())
//            ->setMeta([
//                'count' => $comments->count(),
//            ]);
//    }
}
